//index.js
//获取应用实例
var app = getApp()
Page({
	data: {
		root:'',
		siteroot:app.setting.siteroot,
		att_url:app.setting.att_url,
		account:app.setting.account,
		ShowAuthM:false,
		pagetitle:'我的运势',
		
	},
	onReady: function (){
		
	},
	onPullDownRefresh: function (){
		var the=this;
		var opt={};
		the.onLoad(opt);
		setTimeout(function(){
			wx.stopPullDownRefresh()
		},1000);
	},
	onLoad: function (opt){
		var page=this;
		var ShowAuthM=page.data.ShowAuthM;
		wx.setNavigationBarTitle({'title':page.data.pagetitle});
		var route=page.route;
		page.setData({
			route:route,
		});
		if(opt && opt.isgoshare){
				page.setData({
					ShowGoshare:true,
				});
			}
		if(route){
			var path=route.slice(0,-6).split('/');
			app.access.plugin=path.shift();
			app.access.action=path.join('.');
		}
		if(opt){
			app.access.query =app.util.made_query(opt,true);
			app.access.op=opt.op?opt.op:'';
		}
		app.util.get(app.access,function(pagedata){
			if(!pagedata.member.nickname || !pagedata.member.avatar){
					ShowAuthM=!ShowAuthM
			}
			page.GetImagetmp(pagedata);
			page.setData({
				pagedata:pagedata,
				ShowAuthM:ShowAuthM,
				pluginnav:app.session.menus,
				action:app.access.plugin+'/'+app.access.action+'/'
			});
		});
	},
	info_login:function(e){
		//根据openid为用户登陆，此处尝试获取用户信息
		var the=this;
		app.util.getUserInfo(e,function(res){
			if(res && res.userInfo){
				app.isgetUserInfo=true
				the.setData({
					ShowAuthM:false
				})
				the.onLoad();
			}else{
				return false;
			}
		});
	},
	closeAuthM:function(){
		var the=this;
		the.setData({
			ShowAuthM:false
		})
	},
  	click:function(evt){
		//用户点击事件 data.func 为方法名，data.params 为调用参数！
		var data=evt.currentTarget.dataset;
		app.util[data.func](data.params);
	},
	click_tab:function(evt){
		//用户点击事件 data.func 为方法名，data.params 为调用参数！
		var data=evt.currentTarget.dataset;
		app.util.tab(data.params);
	},
	formSubmit:function(event){
		var data=event.detail.value;
		app.util.post('post',data);
	},
	//画布 
	promisify: api => {
		return (options, ...params) => {
		  return new Promise((resolve, reject) => {
			const extras = {
			  success: resolve,
			  fail: reject
			}
			api({ ...options, ...extras }, ...params)
		  })
		}
	},
	GetImagetmp: function(e){
		var the=this;
		var pagedata=e
		var att_url=the.data.att_url
		const wxGetImageInfo = the.promisify(wx.getImageInfo)
		pagedata.member.avatar=pagedata.member.avatar?pagedata.member.avatar:'../core/resource/images/nopic.jpg'
		pagedata.sale_luck.appcode=pagedata.sale_luck.appcode?pagedata.sale_luck.appcode:siteroot+'/core/resource/images/nopic.jpg'
		pagedata.sharedata.share_image=pagedata.sharedata.share_image?pagedata.sharedata.share_image:siteroot+'/addons/jy_weishop/resource/images/sales/luck_share_image.jpg'
		console.log(pagedata.member.avatar);console.log(pagedata.sale_luck.appcode);console.log(pagedata.sharedata.share_image);
		Promise.all([
			// 商品图
			wxGetImageInfo({
				src: pagedata.sharedata.share_image
			}),
			// 二维码
			wxGetImageInfo({
				src: att_url+pagedata.sale_luck.appcode
			}),
			// 头像
			wxGetImageInfo({
				src: pagedata.member.avatar
			}),
		]).then(res => {
			app.imagetmps=res;
		});
	},
	closesetqrcode: function(){
			var the=this;
			the.setData({
				showsetqrcode:false
			})
	},
	share_canvas: function(){
			var the=this;
			if(app.imagetmps){
				app.util.showLoading();
			}else{
				app.util.msg('绘图忙碌,请稍后尝试！');
				return false;
			}
			setTimeout(function(){the.CanvasContext()},500);
	},
	//绘制画
	CanvasContext: function(){
			var the=this;
			var pagedata=the.data.pagedata
			var att_url=the.data.att_url
			var screenWidth=280
			var screenHeight=app.screenHeight * 0.94
			var titleHight=19
			pagedata.member.nickname=pagedata.member.nickname?pagedata.member.nickname:'匿名用户';
			pagedata.sharedata.share_title=pagedata.sharedata.share_title?pagedata.sharedata.share_title:'我今日运势《'+pagedata.sale_luck.title+'》，快来借势吧,好运势享大折扣！';
			pagedata.sharedata.share_desc=pagedata.sharedata.share_desc?pagedata.sharedata.share_desc:'拥有好运势，不仅仅拥有好心情，更能享受好的购物折扣哦！';
			pagedata.sharedata.share_title1=pagedata.sharedata.share_title.substr(0,16);
			//console.log(pagedata.sharedata.share_title1);
			pagedata.sharedata.share_title2=pagedata.sharedata.share_title.substr(16,16)+'...';
			//console.log(pagedata.sharedata.share_title2);
			const ctx = wx.createCanvasContext('shareCanvas')
			ctx.setFillStyle('white');//填充白色
			ctx.fillRect(0, 0, screenWidth , 390);//画出矩形白色背景
			ctx.restore();
			// 文字1
			//ctx.setTextAlign('center')    // 文字居中
			ctx.setFillStyle('#000000')  // 文字颜色：黑色
			ctx.setFontSize(12)         // 文字字号：px
			//ctx.fillText(pagedata.member.nickname+' ', 60, 25)
			// 文字2
			//ctx.fillText(pagedata.member.time+'推荐了一款商品', 60, 25 + 19)
			// 商品标题
			ctx.fillText(pagedata.sharedata.share_title1+' ', 10,  308)
			ctx.fillText(pagedata.sharedata.share_title2+' ', 10,  328)
			ctx.setFillStyle('red')  // 文字颜色：黑色
			ctx.setFontSize(16)         // 文字字号：px
			ctx.fillText(pagedata.sale_luck.title, 10,  355)
			// 二维码文字
			ctx.setFillStyle('#999')  // 文字颜色：黑色
			//ctx.fillText('扫《'+app.setting.title+'》二维码', screenWidth-270,  295)
			// 二维码文字
			ctx.setFontSize(10)         // 文字字号：px
			ctx.fillText('微信扫一扫', screenWidth-70, 373)
			// 小程序码
			ctx.drawImage(app.imagetmps[1].path, screenWidth-80, 290, 70, 70)
			// 商品图
			ctx.drawImage(app.imagetmps[0].path, 2, 2, screenWidth-4,280)
			// 头像
			//ctx.save()
			//ctx.beginPath()
			//ctx.arc(30, 30, 20, 0,2 * Math.PI);
			//ctx.clip();
			//ctx.setStrokeStyle('#fff');
			//ctx.drawImage(app.imagetmps[2].path,10,5,60,60)
			ctx.restore();
			ctx.draw()
			the.setData({
				showsetqrcode:true
			})
			the.getcanvasToPath()
	},
	//获取海报路径
	getcanvasToPath:function(){
		var the = this;
		var screenWidth=280
		var screenHeight=390
		setTimeout(function () {
			wx.canvasToTempFilePath({
				x: 0,
				y: 0,
				width: screenWidth,
				height: screenHeight,
				destWidth: screenWidth*2.6,
				destHeight: screenHeight*2.6,
				canvasId: 'shareCanvas',
				success: function (res) {
					setTimeout(function () {app.util.hideLoading()},1000);
					the.setData({
						savedImgUrl: res.tempFilePath
					})
				},
				fail: function (res){
					console.log('失败');console.log(res)
					setTimeout(function () {app.util.hideLoading()},1000);
				}
			})
		}, 1000)
	},
	//保存海报
	saveImageToPhoto: function () {
		var that = this;
		setTimeout(function () {
			if (that.data.savedImgUrl != "") {
				wx.saveImageToPhotosAlbum({
					filePath: that.data.savedImgUrl,
					//filePath:'/resource/images/share-bg01.png',
					success: function () {
						wx.showModal({
							title: '保存图片成功',
							content: '图片已经保存到相册，快去分享吧！',
							showCancel: false,
							success: function (res) {
								that.setData({
									showsetqrcode:false,
									canvasShow: false,
								})
							},
							fail: function (res) { },
								complete: function (res) { },
						});
					},
					fail: function(res) {
						//console.log(res);
						if (res.errMsg == "saveImageToPhotosAlbum:fail cancel") {
							wx.showModal({
								title: '保存图片失败',
								content: '您已取消保存图片到相册！',
								showCancel: false
							});
						} else {
							wx.showModal({
								title: '提示',
								content: '保存图片失败，您可以点击确定设置获取相册权限后再尝试保存！',
								complete: function(res) {
									//console.log(res);
									if (res.confirm) {
									wx.openSetting({}) //打开小程序设置页面，可以设置权限
									} else {
									wx.showModal({
										title: '保存图片失败',
										content: '您已取消保存图片到相册！',
										showCancel: false
									});
									}
								}
							});
						}
					}
				})
			}
		}, 1500)
	},
	onShareAppMessage: function (res) {
		var the = this
		var pagedata = the.data.pagedata
		if (res.from === 'button') {
		}
		var path='/jy_weishoppei/index/index?'+app.access.query;
		if(app.access.op){
			path +='&op='+app.access.op;
		}
		if(pagedata.fromuser){
			path +='&fromuser='+pagedata.fromuser;
		}
		if(pagedata.sale_luck){
			path +='&luckkey='+pagedata.sale_luck.luckkey;
		}
		if(pagedata.sale_luck && pagedata.sale_luck.title){
			return {
			  title: pagedata.sharedata.title,
			  path: path,
			  desc: pagedata.sharedata.desc,
			  imageUrl: pagedata.sharedata.imgUrl,
			  success: function(res) {
			  },
			  fail: function(res) {
			  }
			}
		}else{
			return {
			  title: pagedata.sharedata.title,
			  path: path,
			  desc: pagedata.sharedata.desc,
			  success: function(res) {
			  },
			  fail: function(res) {
			  }
			}
		}
	},
	onShareTimeline: function (res) {
		var the = this
		var pagedata = the.data.pagedata
		var query=app.access.query
		if(app.access.op){
			query +='&op='+app.access.op;
		}
		if(pagedata.fromuser){
			query +='&fromuser='+pagedata.fromuser;
		}
		query +='&isgoshare=1'
		return {
		  title: the.data.pagetitle,
		  query: query,
		  desc: app.setting.sharedesc,
		  success: function(res) {
		  },
		  fail: function(res) {
		  }
		}
	},
});